#ifndef LOGINSCENE_H_20120412
#define LOGINSCENE_H_20120412

#include "NetworkDelegate.h"
#include "cocos2d.h"
#include "InlineFunc.h"

#include "CocosUI/CocosUI.h"

const int BTN_LOGIN_LOGIN = 0;
const int BTN_LOGIN_CREATEROOM = 1;
const int BTN_LOGIN_JOINROOM   = 2;
const int BTN_LOGIN_CREATEROLE = 3;
const int BTN_LOGIN_FIGHT      = 4;
const int BTN_LOGIN_CREATEACCOUNT = 5;
const int BTN_LOGIN_LEAVEROOM     = 6;
const int BTN_LOGIN_REGION = 2000;
class CLoginScene : public cocos2d::CCLayer, public CAccountDelegate, public CGameDelegate, public cocos2d::CCIMEDelegate, CUIListener
{
public:
    CLoginScene()
        : m_chooseID(-1)
        , m_pCurTrackNode(NULL)
        , m_lastAdjustVertY(0)
        , m_regionCount(0)
        , m_bCreateRoom(false)
        , m_bInRoom(false)
    {
        m_pServer = CServerHelper::GetInstance();
        m_pServer->SetGameDelegate(this);
        m_pServer->SetAccountDelegate(this);
    }
    
    static void SetConnecting(bool bConnecting) {m_bConnecting = bConnecting;}
    
    bool init();
	void onEnter();

	LAYER_NODE_FUNC(CLoginScene);
	LAYER_SHOW_FUNC(CLoginScene);

    virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
    virtual void keyboardWillShow(cocos2d::CCIMEKeyboardNotificationInfo& info);
    virtual void keyboardWillHide(cocos2d::CCIMEKeyboardNotificationInfo& info);
    virtual void onReceiveMsg(int aID, int event = 0);
    
	void Tick(cocos2d::ccTime dt);
    
private:
    void ShowLogin(bool bShow);
    void ShowRoom(bool bShow);
    void ShowCreateRole(bool bShow);
    //void ShowFight(bool bShow);
    
private:
	virtual void connectServerBack(bool bSuccess);
	virtual void userLoginBack(bool bSuccess, std::vector<RegionInfo > *pRegionList);
	virtual void userRegisterBack(bool bSuccess);
	virtual void chooseRegionBack(bool bSuccess, RegionUser *pUserInfo);
	virtual void accountServerLost();
	virtual void otherReceiveMsg(int id);
	virtual void otherAccountMsg(int id);
    //virtual void createRoomBack(bool bSuccess, RoleInfo *pTargetInfo);
    
    // 返回连接区域信息
    virtual void connectRegionBack(bool bSuccess);
    // 返回登陆区域信息，如果成功，可以进行游戏了
    virtual void loginRegionBack(bool bSuccess);
    // 需要创建一个人物
    virtual void regionNoRole();
	virtual void createNameRepeat();
    virtual void startFightBack(bool bSuccess, const anFight &p) ;
    virtual void userLost() {};
	virtual void recvChatFromName(const char *szName, const char *szMsg, int len);
    virtual void gameServerLost();

private:
    CServerHelper          *m_pServer;
	cocos2d::CCLabelBMFont *m_pLable;
    
    cocos2d::CCTextFieldTTF *m_pTFName;
    cocos2d::CCTextFieldTTF *m_pTFPsw;
    cocos2d::CCTextFieldTTF *m_pTFRoleName;
    cocos2d::CCTextFieldTTF *m_pCurTrackNode;
    cocos2d::CCTextFieldTTF *m_pTFRoleType;

	cocos2d::CCLabelTTF     *m_pLableTest;
	cocos2d::CCLabelTTF     *m_pLabelMsg;
    
    float                    m_lastAdjustVertY;
    int                      m_regionCount;
    ControlVector            m_vBtnRegions;
    vector<int>              m_vRegionIDs;
    bool                     m_bCreateRoom;
    bool                     m_bInRoom;
    
    CCocosButton            *m_pBtnLogin;
    CCocosButton            *m_pBtnCreateAccount;
    CCocosButton            *m_pBtnCreateRoom;
    //CCocosButton            *m_pBtnJoinRandowRoom;
    CCocosButton            *m_pBtnCreateRole;
    //CCocosButton            *m_pBtnFight;
    //CCocosButton            *m_pBtnLeaveRoom;
    
    int m_chooseID;
    
    static bool              m_bConnecting;
};
#endif